package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.controller.yxsms.model.AddYxSmsModel;
import com.ls.controller.yxsms.model.EditYxSmsModel;
import com.ls.controller.yxsms.model.QueryYxSmsModel;
import com.ls.controller.yxsms.model.YxSmsVo;
import com.ls.entity.*;
import com.ls.mapper.YxSmsMapper;
import com.ls.service.ICompanyService;
import com.ls.service.IYxFieldService;
import com.ls.service.IYxSmsService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.ls.constant.SystemConstants.NUMBER_0;
import static com.ls.constant.SystemConstants.NUMBER_1;

/**
 * <p>
 * 5g阅信模板 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class YxSmsServiceImpl extends ServiceImpl<YxSmsMapper, YxSms> implements IYxSmsService {
    private final ICompanyService companyService;
    private final IYxFieldService yxFieldService;
    /**新增*/
    @Override
    public ResultData add(AddYxSmsModel model) {
        log.info("新增5g阅信模板：{}", model);
        if (model.getYxSmsType()==NUMBER_1&&model.getCompanyId()==null){
            return ResultData.failure("公司id不能为空");
        }
        YxSms yxSms=new YxSms();
        BeanUtils.copyProperties(model,yxSms);
        saveOrUpdate(yxSms);
        if (model.getList()!=null&&model.getList().size()>0){
            for (AddYxSmsModel.info info : model.getList()) {
                YxField yxField=new YxField();
                BeanUtils.copyProperties(info,yxField);
                yxField.setYxSmsId(yxSms.getYxSmsId());
                yxField.insert();
            }
        }
        return ResultData.success();
    }
    /** 更新  */
    @Override
    public ResultData edit(EditYxSmsModel model) {
        log.info("更新5g阅信模板：{}", model);
        if (model.getYxSmsType()==NUMBER_1&&model.getCompanyId()==null){
            return ResultData.failure("公司id不能为空");
        }
        YxSms yxSms=new YxSms();
        BeanUtils.copyProperties(model,yxSms);
        yxFieldService.remove(new LambdaQueryWrapper<YxField>().eq(YxField::getYxSmsId, model.getYxSmsId()));
        saveOrUpdate(yxSms);
        if (model.getList()!=null&&model.getList().size()>0){
            for (EditYxSmsModel.info info : model.getList()) {
                YxField yxField=new YxField();
                BeanUtils.copyProperties(info,yxField);
                yxField.setYxSmsId(yxSms.getYxSmsId());
                yxField.insert();
            }
        }
        return ResultData.success();
    }
    /** 删除  */
    @Override
    public ResultData del(Long yxSmsId) {
        log.info("删除5g阅信模板：{}", yxSmsId);
        YxSms yxSms=getById(yxSmsId);
        if (yxSms==null){
            return ResultData.failure("5g阅信模板不存在");
        }
        yxFieldService.remove(new LambdaQueryWrapper<YxField>().eq(YxField::getYxSmsId, yxSmsId));
        return yxSms.deleteById()?ResultData.successMsg("删除成功"):ResultData.failure("删除失败");
    }
    /**根据id查询*/
    @Override
    public ResultData getYxSmsById(Long yxSmsId) {
        YxSms yxSms = getById(yxSmsId);
        return ResultData.successData(yxSms);
    }
    /**
     * 查询5g阅信模板分页列表
     */
    @Override
    public ResultData pageList(QueryYxSmsModel model) {
        List<YxSms> yxSmsList = list(new LambdaQueryWrapper<YxSms>()
                .eq(YxSms::getYxSmsType, model.getYxSmsType())
                .eq(model.getCompanyId()!=null,YxSms::getCompanyId,model.getCompanyId())
                .orderByAsc(YxSms::getCreateTime));

        List<YxSmsVo> resultList = yxSmsList.stream().map(yxSms ->  {
            YxSmsVo yxSmsVo = new YxSmsVo();
            BeanUtils.copyProperties(yxSms,yxSmsVo);
            List<YxField> yxFieldList = yxFieldService.list(new LambdaQueryWrapper<YxField>()
                    .eq(YxField::getYxSmsId, yxSms.getYxSmsId()));
            List<YxSmsVo.info> info = yxFieldList.stream().map(yxField -> {
                YxSmsVo.info info1 = new YxSmsVo.info();
                BeanUtils.copyProperties(yxField,info1);
                return info1;
            }).collect(Collectors.toList());
            yxSmsVo.setList(info);
            return yxSmsVo;
        }).collect(Collectors.toList());

        return ResultData.successData(resultList);
    }
    /**查询当前用户的所有模板*/
    @Override
    public ResultData getPaging(Long companyId) {
        LambdaQueryWrapper<YxSms> wrapper = new LambdaQueryWrapper<YxSms>()
                .and(q -> q
                        .eq(YxSms::getYxSmsType, NUMBER_0)
                        .or(companyId != null,
                                subQ -> subQ
                                        .eq(YxSms::getYxSmsType, NUMBER_1)
                                        .eq(YxSms::getCompanyId, companyId)
                        )
                );

        List<YxSms> yxSmsList = list(wrapper);
        return ResultData.successData(yxSmsList);
    }
}
